package com.mdc.mansion.web.modules.system.web;

import java.util.HashMap;
import java.util.Map;

import com.mdc.mansion.core.modules.system.SysRoleService;
import com.mdc.mansion.dataaccess.common.persistence.Page;
import com.mdc.mansion.dataaccess.modules.system.entity.Role;
import com.mdc.mansion.web.modules.system.service.SystemService;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import com.mdc.mansion.web.common.web.BaseController;

/**
 * 角色Controller
 * @author chenmingdong
 * @version 2016-03-07
 */
@Controller
@RequestMapping(value = "${adminPath}/system/role")
public class RoleController extends BaseController {

	@Autowired
	private SysRoleService sysRoleService;
	
	@Autowired
	private SystemService systemService;
	
	@ModelAttribute
	public Role get(@RequestParam(required=false) String id) {
		if (StringUtils.isNotBlank(id)){
			return systemService.getRole(id);
		}else{
			return new Role();
		}
	}
	
	@RequiresPermissions("system:role:view")
	@RequestMapping(value = {"list", ""})
	public ModelAndView list(Role role) {
		Map<String, Object> model=new HashMap<String, Object>();
		Page<Role> page = sysRoleService.findPage(role);
        model.put("page", page);
		ModelAndView modelAndView=new ModelAndView("modules/system/roleList", model);
		return modelAndView;
	}
	
	@RequiresPermissions("system:role:view")
	@RequestMapping(value = "form")
	public ModelAndView form(Role role) {
		Map<String, Object> model=new HashMap<String, Object>();
		model.put("role", role);
		model.put("menuList", systemService.findAllMenu());
		return new ModelAndView("modules/system/roleForm",model);
	}
	

	@RequiresPermissions("system:role:edit")
	@RequestMapping(value = "save")
	public ModelAndView save(Role role,RedirectAttributes redirectAttributes) {
		systemService.saveRole(role);
		this.addMessage(redirectAttributes, "操作成功");
		return new ModelAndView("redirect:./list");
	}
	
	@RequiresPermissions("system:role:edit")
	@RequestMapping(value = "delete")
	public ModelAndView delete(Role role,RedirectAttributes redirectAttributes) {
		sysRoleService.delete(role);
		this.addMessage(redirectAttributes, "操作成功");
		return new ModelAndView("redirect:./list");
	}
	
}
